/**
 * 
 */
package com.cmpanyandi.elearning.utils.db;

import java.util.Map.Entry;

import android.content.ContentValues;
import android.database.Cursor;

import com.cmpanyandi.elearning.utils.StringUtils;

/**
 * This bean allows to be persist in DB.
 * 
 * @author wojtek
 * 
 */
public abstract class DBBean {

    public static final String PK = "_id";

    private Long pk;
    protected ContentValues contentValues = new ContentValues();

    public enum SqlOperator {

    }

    /**
     * Return database primary key.
     * 
     * @return pk of bean
     */
    public Long getPk() {
	return pk;
    }

    public void setPk(Long pk) {
	if (pk != null && pk.longValue() > 0) {
	    contentValues.put(PK, pk);
	}
	this.pk = pk;
    }

    /**
     * This method return true if bean should be inserted during persistence.
     * 
     * @return - true if should be inserted false if updated.
     */
    public boolean isInsertRequired() {
	return pk == null || pk.longValue() < 1 ? true : false;
    }

    /**
     * This method get where clause prepare statement string.
     * 
     * @return
     */
    public String getWherePrepareStmtClause() {
	String stmt = "";

	for (Entry<String, Object> value : contentValues.valueSet()) {
	    //TODO - change this to queryObject to avoid binding with 
	    if ("name".equals(value.getKey())) {
		
		stmt += value.getKey() + " like ? and ";
	    } else {
		stmt += value.getKey() + " = ? and ";
	    }
	}

	if (StringUtils.isNotEmpty(stmt)) {
	    stmt = stmt.substring(0, stmt.length() - 4);
	}
	return stmt;
    }

    /**
     * This method returns arguments for search query which corresponded to
     * prepare statement.
     * 
     * @return - array of arguments for prepare statement.
     */
    public String[] getWherePrepareStmtArgs() {
	String[] args = new String[contentValues.size()];
	int i = 0;

	for (Entry<String, Object> value : contentValues.valueSet()) {
	    args[i] = value.getValue().toString();
	    i++;
	}

	return i > 0 ? args : null;
    }

    /**
     * This method return bean uniq identify clause based on PK.
     * 
     * @return e.g. pk=1;
     */
    public String getIdentityWhereClause() {
	return PK + "=" + getPk();
    }

    /**
     * Return value of selection columns from bean as Array.
     * 
     * @return array of all columns names require to create bean.
     */
    public abstract String[] getSelectionColumns();

    /**
     * This name returns order by column sequence.
     * 
     * @return - order by which apply to search.
     */
    public abstract String getOrderBy();

    /**
     * This method initialise bean from cursor.
     * 
     * @param cursor
     */
    public void initFromCursor(Cursor cursor) {
	
	setPk(cursor.getLong(cursor.getColumnIndexOrThrow(PK)));
    }

    /**
     * This method return table name;
     * 
     * @return the tableName
     */
    public abstract String getTableName();

    /**
     * @return the contentValues
     */
    public ContentValues getContentValues() {
	return contentValues;
    }

}
